package com.haoting.blogservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haoting.blogservice.entity.Comments;
import com.haoting.blogservice.entity.vo.CommentVo;
import com.haoting.blogservice.service.CommentService;
import com.haoting.commonutils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tingting
 * @since 2020-12-21
 */
@RestController
@RequestMapping("/blogservice/comment")
//@CrossOrigin
public class CommentController {

    @Autowired
    CommentService commentService;

    @PostMapping("addComment")
    public CommonResult addComment(@RequestBody Comments comment){

        boolean save = commentService.save(comment);
        if(save){
            return CommonResult.ok();
        }else {
            return CommonResult.error();
        }
    }

    @GetMapping("getComment/{blogId}")
    public CommonResult getComment(@PathVariable String blogId){
        List<Comments> comments = commentService.getComment(blogId);
        return CommonResult.ok().data("comments",comments);
    }

    @GetMapping("getCommentInfo/{id}")
    public CommonResult getCommentInfo(@PathVariable String id){
        Comments comment = commentService.getById(id);
        return CommonResult.ok().data("comments",comment);
    }


    @GetMapping("getCommentClid/{blogId}")
    public CommonResult getCommentClid(@PathVariable String blogId){
        List<Comments> comments = commentService.getCommentClid(blogId);
        return CommonResult.ok().data("commentclids",comments);
    }
    @DeleteMapping("deleteComment/{id}")
    public CommonResult deleteComment(@PathVariable String id){
        commentService.removeById(id);
        return CommonResult.ok();
    }
}

